package com.wc.算法提高课.C第三章_图论.差分约束.雇佣收银员;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/18 13:20
 * @description https://www.acwing.com/problem/content/395/
 */
public class Main {
    /**
     * 思路: 差分约束<p>
     * nums[i] 表示可以从 i - 1 开始有多少个人<p>
     * x[i] 表示 i - 1 时刻有几个人 <p>
     * s[i] 表示 1 ~ i 一共有几个人 s[i] = x[1] + ... + x[i]<p>
     * 0 <= x[i] <= nums[i] => 0 <= s[i] - s[i - 1] <= nums[i]<p>
     * x[i - 7] + x[i - 6] + ... + x[i] >= r[i] <p>
     * => i >= 8, s[i] - s[i - 8] >= r[i]<p>
     * => i < 8, s[i] + s[24] - s[i + 16] >= r[i]<p>
     * => s[24]不固定, 二分枚举<p>
     * =>为了s[24] 固定, s[24] >= c, s[24] <= c<p>
     * 题目为最少, 那就是最长路<p>
     * s[i] >= s[i - 1]<p>
     * s[i - 1] >= s[i] - nums[i]<p>
     * s[i] >= s[i - 8] + r[i] (i >= 8)<p>
     * s[i] >= s[i + 16] - s[24] + r[i](i < 8)<p>
     * s[24] >= c, 0 >= s[24] - c
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 26, M = 110, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] dist = new int[N], cnt = new int[N], q = new int[N], r = new int[N], nums = new int[N];
    static boolean[] st = new boolean[N];
    static int n, idx = 1;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            for (int i = 1; i <= 24; i++) r[i] = sc.nextInt();
            n = sc.nextInt();
            Arrays.fill(nums, 0);
            for (int i = 0; i < n; i++) {
                int x = sc.nextInt();
                nums[x + 1]++;
            }
            int l = 0, r = n;
            while (l < r) {
                int mid = l + r >> 1;
                if (spfa(mid)) r = mid;
                else l = mid + 1;
            }
            if (spfa(l)) out.println(l);
            else out.println("No Solution");
        }
        out.flush();
    }

    static boolean spfa(int c) {
        build(c);
        Arrays.fill(dist, -INF);
        Arrays.fill(st, false);
        Arrays.fill(cnt, 0);
        dist[0] = 0;
        int hh = 0, tt = 1;
        q[0] = 0;
        st[0] = true;
        while (hh != tt) {
            int t = q[hh++];
            if (hh == N) hh = 0;
            st[t] = false;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] < dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    cnt[j] = cnt[t] + 1;
                    if (cnt[j] >= 25) return false;
                    if (!st[j]) {
                        q[tt++] = j;
                        if (tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }

        return true;
    }

    static void build(int c) {
        Arrays.fill(h, 0);
        idx = 1;
        for (int i = 1; i <= 24; i++) {
            add(i - 1, i, 0);
            add(i, i - 1, -nums[i]);
        }
        for (int i = 8; i <= 24; i++) add(i - 8, i, r[i]);
        for (int i = 1; i < 8; i++) add(i + 16, i, -c + r[i]);

        add(0, 24, c);
        add(24, 0, -c);
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
